﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Linq.Expressions;

public partial class zoekresultaat : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        string zoekopdr = Request.QueryString["zoekopdracht"];
        Label3.Text += zoekopdr;
        zoekopdracht(zoekopdr);
    }

    //pagina nummers regelen voor gridview1
    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    //pagina nummers regelen voor gridview2
    protected void gridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        GridView2.DataBind();
    }

    protected void zoekopdracht(string tekst)
    {
        if (tekst == null)
            tekst = "niks";
        string[] woorden = tekst.Split(' ');

        var titels = ContainsTitel(tekst);
        var makers = ContainsMaker(tekst);

        var titel2 =
            from g in db.Games.Where(titels)
            join m in db.Makers on g.MAKER_ID equals m.MAKER_ID
            join p in db.Platforms on g.PLATFORM_ID equals p.PLATFORM_ID
            select new { g.GAME_NR, g.TITEL, m.MAKER_ID, m.NAAM, p.OMSCHRIJVING, g.PRIJS, g.IMAGE };

        var makers2 =
            from m in db.Makers.Where(makers)
            join g in db.Games on m.MAKER_ID equals g.MAKER_ID
            join p in db.Platforms on g.PLATFORM_ID equals p.PLATFORM_ID
            select new { g.GAME_NR, g.TITEL, m.MAKER_ID, m.NAAM, p.OMSCHRIJVING, g.PRIJS, g.IMAGE };

        if (makers2.Count() == 0 && titel2.Count() == 0)
        {
            var platforms = ContainsPlatform(tekst);

            var platforms2 =
                from g in db.Games
                join m in db.Makers on g.MAKER_ID equals m.MAKER_ID
                join p in db.Platforms.Where(platforms) on g.PLATFORM_ID equals p.PLATFORM_ID
                select new { g.GAME_NR, g.TITEL, m.MAKER_ID, m.NAAM, p.OMSCHRIJVING, g.PRIJS, g.IMAGE };

            GridView1.DataSource = platforms2;
            GridView1.DataBind();
            Label2.Text = "";
        }
        else
        {
            GridView1.DataSource = titel2;
            GridView1.DataBind();
            GridView2.DataSource = makers2;
            GridView2.DataBind();

        }
    }

    public static Expression<Func<Game, bool>> ContainsTitel(
                                                string keywords)
    {
        var predicate = PredicateBuilder.False<Game>();
        string[] woorden = keywords.Split(' ');
        foreach (string woord in woorden)
        {
            if (woord != "")
            {
                string temp = woord;
                predicate = predicate.Or(g => g.TITEL.Contains(temp));
            }
        }
        return predicate;
    }
    public static Expression<Func<Maker, bool>> ContainsMaker(
                                            string keywords)
    {
        var predicate = PredicateBuilder.False<Maker>();
        string[] woorden = keywords.Split(' ');
        foreach (string woord in woorden)
        {
            if (woord != "")
            {
                string temp = woord;
                predicate = predicate.Or(m => m.NAAM.Contains(temp));
            }
        }
        return predicate;
    }

    public static Expression<Func<Platform, bool>> ContainsPlatform(
                                           string keywords)
    {
        var predicate = PredicateBuilder.False<Platform>();
        string[] woorden = keywords.Split(' ');
        foreach (string woord in woorden)
        {
            if (woord != "" && woord != " ")
            {
                string temp = woord;
                predicate = predicate.Or(p => p.OMSCHRIJVING.Contains(temp));
            }
            else
            {
                predicate = predicate.Or(p => p.OMSCHRIJVING != null);
            }
        }
        return predicate;
    }
}
